package com.ymatou.iapi.godmch.testcase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oracle.net.aso.a;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.alibaba.druid.sql.visitor.functions.If;
import com.ymatou.iapi.godmch.parameter.AgentInfo;
import com.ymatou.iapi.godmch.parameter.AgentListBean;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godmch.parameter.registerByCodeBean;
import com.ymatou.iapi.godmch.service.AgentListCall;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.getAgentPromotePicCall;
import com.ymatou.iapi.godmch.service.registerByCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
/**
*代理商列表
*FreeMaker Template自动生成代码
*/
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
@TestSuite
public class Ts_AgentList{
	private static AgentListBean agentlistBean;
	private static AgentListCall agentlistCall;
	DefGodMchWapper DefGodMchWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("代理商列表");
	}

	@Before
	public void caseUp() {
		agentlistBean=new AgentListBean();
		agentlistCall=new AgentListCall();
		DefGodMchWapper=new DefGodMchWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(agentlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void VerifyDb(CreateCodeBean ccbean) throws InterruptedException {
		ArrayList<AgentInfo> a = agentlistCall.getAgents();
		for (AgentInfo agentInfo : a) {
			Logger.verifyEquals(1,DefGodMchWapper.selectMchRelation(ccbean.getMchId(), agentInfo.getAgentId(), agentInfo.getAgentType()).size(),"验证商户和代理关系正确");
			agent_type atDbInfo = DefGodMchWapper.selectAgentType(ccbean.getMchId(), ccbean.getRoleType()).get(0);
			Logger.verifyEquals(atDbInfo.getAgentType(), agentInfo.getAgentType(), "验证AgentType");
			Logger.verifyEquals(atDbInfo.getTypeTitle(), agentInfo.getAgentTypeTitle(), "验证AgentTypeTitle");
			//待确认展示内容
			Logger.verifyEquals("title", agentInfo.getAgentName(), "验证AgentName");
			Logger.verifyEquals(DGUserCallService.SellerDefImg, agentInfo.getAvatar(), "验证Avatar");
			
			//AgentModel
			Integer agentModel=ccbean.getAgentModel()==null?1:ccbean.getAgentModel();
			Logger.verifyEquals(agentModel, agentInfo.getAgentModel(), "验证AgentModel");
			//agentModelText
			if(agentModel==1)
				Logger.verifyEquals("自主选货", agentInfo.getAgentModelText(), "验证AgentModelText");
			else if(agentModel==2)
				Logger.verifyEquals("自动继承", agentInfo.getAgentModelText(), "验证AgentModelText");
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AgentList_001() {
		Logger.start(true,"获取代理商列表成功");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			GodmchManageCallService.editMchInfo(agentid, "title",null,null);
			agentlistBean.setMchId(ccbean.getMchId());
			agentlistBean.setAgentModel(1);
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();	
			
			Logger.verifyEquals("200", agentlistCall.getString("status"), "验证status");
			Logger.verifyEquals(1, agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证total");
			Logger.verifyEquals(agentlistCall.getAgents().size(), agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证totalSize");
			
			//新增 2000819
			Logger.verifyEquals(agentlistCall.getAgents().get(0).getInviteNum(), agentlistCall.getJsonObject().get("result").
					getAsJsonObject().get("agents").getAsJsonArray().get(0).getAsJsonObject().get("inviteNum").getAsInt(), "验证inviteNum");
			
			VerifyDb(ccbean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AgentList_001_4() {
		Logger.start(true,"获取代理商列表成功-代理招代理");
		try {

			Integer mchId=501371929;
			//获取主商户agentType
			agent_typeWapper agentTypewapper = new agent_typeWapper();
			Map<String, Object> map = new HashMap();
			map.put("mchId", mchId);
			List<agent_type> agenttypeList = agentTypewapper.selectby(map);
			
			//申请 审核代理1
			applyAgentCall applyAgent =GodmchManageCallService.applyAndapproveAgent(mchId);
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(applyAgent.getAgentId());
            GodmchManageCallService.editmachCall(editmchBean);
			
			getAgentPromotePicCall getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(applyAgent.getAgentId());
			Thread.sleep(5000);
			registerByCodeBean registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerByCodeCall registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId2=registerCall.getRegMchId();
			Logger.comment("################ agentId2:"+agentId2);
			
			//申请 审核代理2
			editmchBean=GodmchManageCallService.IniteditmachBean(registerCall.getRegMchId());
            GodmchManageCallService.editmachCall(editmchBean);
		    registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
		    registerCall=GodmchManageCallService.registerByCodeCall(registerBean);

			
			agentlistBean.setMchId(mchId);
			agentlistBean.setAgentModel(1);
			agentlistBean.setPageIndex(1);
			agentlistBean.setPageSize(5);
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();	
				
			//新增 2000819
			agentlistCall.getAgents().stream().forEach(a->{
				try {
					if(a.getAgentId().equals(applyAgent.getAgentId()))
					{
						Logger.verifyEquals(a.getInviteNum(), 2, "验证inviteNum");
					}
				} catch (Exception e) {
					e.printStackTrace();
				 }
		    	}
			);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AgentList_001_3() {
		Logger.start(true,"获取代理商列表成功-验证绑定关系后商户修改代理类型,(关系表mch_relation中的agentMode=1,然后修改agent_type表中的agentMode=2)");
		try {

			Integer mchid=500957190;
			agentlistBean.setMchId(mchid);
			agentlistBean.setAgentModel(1);
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();	
			
			Logger.verifyEquals("200", agentlistCall.getString("status"), "验证status");
			Logger.verifyEquals(0, agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证total");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AgentList_001_1() {
		Logger.start(true,"获取代理商列表成功-代理模式 1自主选货");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			GodmchManageCallService.editMchInfo(agentid, "title",null,null);
			agentlistBean.setMchId(ccbean.getMchId());
			agentlistBean.setAgentModel(1);
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();			
			Logger.verifyEquals("200", agentlistCall.getString("status"), "验证status");
			Logger.verifyEquals(1, agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证total");
			Logger.verifyEquals(agentlistCall.getAgents().size(), agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证totalSize");
			VerifyDb(ccbean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_AgentList_001_2() {
		Logger.start(true,"获取代理商列表成功-代理模式 2.自动继承 20191015修改 继承型代理目前仅支持中国大陆商户");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setAgentModel(2);//2.自动继承
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			GodmchManageCallService.editMchInfo(agentid, "title",null,null);
			agentlistBean.setMchId(ccbean.getMchId());
			agentlistBean.setAgentModel(2);
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();			
			Logger.verifyEquals("200", agentlistCall.getString("status"), "验证status");
			Logger.verifyEquals(1, agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证total");
			Logger.verifyEquals(agentlistCall.getAgents().size(), agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证totalSize");
			VerifyDb(ccbean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AgentList_002() {
		Logger.start(true,"获取多个代理商列表成功");
		try {
			int count=3;
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			for(int i=0;i<count;i++) {
				Integer agentid = GodmchManageCallService.CreateSellerMch(code);
				GodmchManageCallService.editMchInfo(agentid, "title",null,null);
			}
			agentlistBean.setMchId(ccbean.getMchId());
			agentlistBean.setPageSize(5);
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();			
			Logger.verifyEquals("200", agentlistCall.getString("status"), "验证status");
			Logger.verifyEquals(count, agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证total");
			Logger.verifyEquals(agentlistCall.getAgents().size(), agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证totalSize");
			VerifyDb(ccbean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AgentList_003() {
		Logger.start(true,"商户没有设置代理商获取空");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			agentlistBean.setMchId(ccbean.getMchId());
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();			
			Logger.verifyEquals("200", agentlistCall.getString("status"), "验证status");
			Logger.verifyEquals(0, agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证total");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AgentList_004() {
		Logger.start(true,"商户id不正确获取空");
		try {
			agentlistBean.setMchId(-1);
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();			
			Logger.verifyEquals("200", agentlistCall.getString("status"), "验证status");
			Logger.verifyEquals(0, agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证total");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AgentList_005() {
		Logger.start(true,"不输入商户id失败");
		try {
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();			
			Logger.verifyEquals("4001", agentlistCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AgentList_006() {
		Logger.start(true,"设置pageindex和pagesize分页查询成功");
		try {
			int count=3;
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			for(int i=0;i<count;i++) {
				Integer agentid = GodmchManageCallService.CreateSellerMch(code);
				GodmchManageCallService.editMchInfo(agentid, "title",null,"wechat");
			}
			for(int i=0;i<count;i++) {
				agentlistBean.setMchId(ccbean.getMchId());
				agentlistBean.setPageIndex(i+1);
				agentlistBean.setPageSize(1);
				agentlistCall.setData(agentlistBean);
				agentlistCall.callService();			
				Logger.verifyEquals("200", agentlistCall.getString("status"), "验证status");
				Logger.verifyEquals(count, agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证total");
				Logger.verifyEquals(agentlistBean.getPageSize(), agentlistCall.getAgents().size(), "验证totalSize");
				VerifyDb(ccbean);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AgentList_007() {
		Logger.start(true,"设置mchid和agentType查询成功");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			GodmchManageCallService.editMchInfo(agentid, "title",null,"wechat");
			Integer type1 = ccbean.getRoleType();
			
			agentlistBean.setAgentType(type1);
			agentlistBean.setMchId(ccbean.getMchId());
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();			
			Logger.verifyEquals("200", agentlistCall.getString("status"), "验证status");
			Logger.verifyEquals(1, agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证total");
			Logger.verifyEquals(agentlistCall.getAgents().size(), agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证totalSize");
			VerifyDb(ccbean);
			
			List<Integer> at = GodmchManageCallService.getNotMyAgentType(ccbean.getMchId(), ccbean.getRoleType());
			ccbean.setRoleType(at.get(0));
			String code2 = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid2 = GodmchManageCallService.CreateSellerMch(code2);
			GodmchManageCallService.editMchInfo(agentid2, "title",null,"wechat");
			Integer type2 = ccbean.getRoleType();
			
			agentlistBean.setAgentType(type2);
			agentlistBean.setMchId(ccbean.getMchId());
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();
			Logger.verifyEquals("200", agentlistCall.getString("status"), "验证status");
			Logger.verifyEquals(1, agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证total");
			Logger.verifyEquals(agentlistCall.getAgents().size(), agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证totalSize");
			VerifyDb(ccbean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AgentList_008() {
		Logger.start(true,"设置sort查询排序顺序查询成功");
		try {
			
			int count=3;
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			for(int i=0;i<count;i++) {
				Integer agentid = GodmchManageCallService.CreateSellerMch(code);
				GodmchManageCallService.editMchInfo(agentid, "title",null,null);
				Thread.sleep(1000);
			}
			
			agentlistBean.setMchId(ccbean.getMchId());
			agentlistBean.setSort(1);
			agentlistBean.setPageSize(5);
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();			
			Logger.verifyEquals("200", agentlistCall.getString("status"), "验证status");
			Logger.verifyEquals(count, agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证total");
			Logger.verifyEquals(agentlistCall.getAgents().size(), agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证totalSize");
			ArrayList<AgentInfo> a1 = agentlistCall.getAgents();
			agentlistBean.setMchId(ccbean.getMchId());
			agentlistBean.setSort(2);
			agentlistCall.setData(agentlistBean);
			agentlistCall.callService();			
			Logger.verifyEquals("200", agentlistCall.getString("status"), "验证status");
			Logger.verifyEquals(count, agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证total");
			Logger.verifyEquals(agentlistCall.getAgents().size(), agentlistCall.getJsonObject().get("result").getAsJsonObject().get("total").getAsInt(), "验证totalSize");
			VerifyDb(ccbean);
			ArrayList<AgentInfo> a2 = agentlistCall.getAgents();
			//验证倒序
			
			Collections.reverse(a2);
			for (int i = 0; i < a1.size(); i++) {
				Logger.verifyEquals(a1.get(i).getAgentId(), a2.get(i).getAgentId(), "验证倒序");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}